<template>
  <div class="clazz-container">
    <el-card class="box-card">
      <div class="search">
        <div class="item">
          <span style="margin-right:5px;">班级名称</span>
          <el-input 
              placeholder="请输入班级名称"
              style="width: 300px"
              v-model="query.name"
              clearable 
              @clear="getClazzList"></el-input>
        </div>
        <el-button type="primary" class="searchBtn" @click="getClazzList">查询</el-button>
        <el-button type="primary" class="addBtn" icon="el-icon-plus" @click="addClazz">添加班级</el-button>
      </div>
      <div class="table">
        <el-table
            width="100%"
            :data="tableData"
            stripe border>
            <el-table-column
              prop="id"
              label="id"
              width="60px">
            </el-table-column>
            <el-table-column
              prop="name"
              label="班级名称">
            </el-table-column>
            <el-table-column
              prop="command"
              label="班级口令"
              width="260px">
            </el-table-column>
            <el-table-column
              prop="num"
              label="班级人数">
            </el-table-column>
            <el-table-column label="创建时间" width="180px">
              <template slot-scope="scope">
                  {{scope.row.createTime | dateFormat}}
              </template>
            </el-table-column>
            <el-table-column label="操作" >
                <template slot-scope="scope">
                  <div class="btns">
                    <el-button class="btn" @click="editClazz(scope.row.id)">编辑</el-button>
                    <el-button class="btn" @click="deleteClazz(scope.row.id)">删除</el-button>
                  </div>
                </template>
            </el-table-column>
          </el-table>
          <!-- 分页区域 -->
          <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                class="pagination"
                :current-page="query.pageIndex"
                :page-sizes="[1, 2, 5, 10]"
                :page-size="query.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
          </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data(){
    return {
      query:{
        pageIndex: 1,
        pageSize: 5,
        name: ""
      },
      tableData:[], //列表数据
      total: 0 //总数

    }
  },
  created(){
    this.getClazzList();
  },
  methods:{
    //查询班级列表
    getClazzList(){
      this.getRequest("/api/teacher/classes/page",{
        pageIndex: this.query.pageIndex,
        pageSize: this.query.pageSize,
        name: this.query.name
      }).then( resp=>{
        if(resp){
          this.tableData = resp.data;
          this.total = resp.total;
        }
      })
    },
    //监听pagesize改变的事件
    handleSizeChange(newSize) {
        //console.log(newSize);
        this.query.pageSize = newSize
        this.getClazzList();
    },
    //监听页码值改变的事件
    handleCurrentChange(newPage) {
        //console.log(newPage);
        this.query.pageIndex = newPage
        this.getClazzList();
    },
    //删除操作
    deleteClazz(id){
      this.$confirm('此操作将删除该班级, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.deleteRequest("/api/teacher/classes/delete/"+id).then(resp=>{
              if(resp.code == 200){ //删除成功
                //重新获取用户列表
                this.getClazzList();
              }
            })
        }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消操作'
            });          
        });
    },
    editClazz(id){
      this.$store.commit("initClazzId",id);
      this.$router.replace("/teacher/clazz/create");
    },
    //班级添加
    addClazz(){
      this.$router.replace("/teacher/clazz/create");
    }
  }
}
</script>

<style lang="scss" scoped>
.clazz-container{
  font-family: "华文楷体";
  .box-card{
     border-radius: 10px;
     color:#1A1A1A;
     .search{
        margin-top: 10px;
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        .item{
          margin: 10px 10px;
          
        }
        .searchInput{
          margin-left: 10px;
          border:1px solid #1980FF;
          border-radius: 4px;
        }
        .searchBtn{
          font-family: "华文楷体";
          margin: 0 0 0 20px;
        }
        .addBtn{
          font-family: "华文楷体";
        }
     }
     .table{
         margin-top: 10px;
         width: 100%;
         //内容居中
         /deep/.el-table th > .cell {
              text-align: center;
          }
          
          /deep/.el-table .cell {
              text-align: center;
          } 
          .btns{
            display: flex;
            justify-content: space-around;
          }
          .btn{
            border: none;
            font-size: 12px;
            margin:0 10px;
            padding: 0;
            cursor: pointer;
            &:nth-child(1){
              color:#1980FF;
            }
             &:nth-child(2){
              color:#F56C6C;
            }
          }
      }
      .pagination{
        margin: 20px 0 0 0;
        display: flex;
        justify-content: flex-end;
      }
  }
}
</style>